<?php

namespace App\Http\Controllers\Inspection;

use Exception;
use App\Models\Image;
use Illuminate\Http\Request;
use App\Models\Inspection\Node;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use App\Repositories\ImageRepository;

/**
 * 巡检节点逻辑控制器
 *
 * Date: 23/04/2018
 * @author George
 * @package App\Http\Controllers\Inspection
 */
class NodeController extends Controller
{
    /**
     * 获取节点数据集合
     *
     * Date: 22/04/2018
     * @author George
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Request $request)
    {
        $user = Auth::user();
        $project_id = $request->get('project_id', $user->project_id);
        $paginate = $request->get('paginate', 15);
        $query = Node::query();
        $nodes = $query->where('project_id', $project_id)->paginate($paginate);
        return success($nodes);
    }

    /**
     * 获取巡检节点详情
     *
     * Date: 22/04/2018
     * @author George
     * @param Node $node
     * @return \Illuminate\Http\JsonResponse
     */
    public function show(Node $node)
    {
    	$node->images = $node->images()->get();
        return success($node);
    }

    /**
     * 创建巡检节点
     *
     * Date: 22/04/2018
     * @author George
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function store(Request $request)
    {
        $attributes = $this->validate($request, [
            'project_id' => 'required',
            'name' => 'required',
            'location' => 'required',
            'images' => 'nullable|array'
        ]);

        $attributes['company_id'] = Auth::user()->company_id;

        $images = array_pull($attributes, 'images');

        try {
            $node = Node::create($attributes);
			if ($images) {
				$imagesStore = new ImageRepository();
				$user = Auth::user();
				$node->images = $imagesStore->store($images, $user->id, $user->company_id, $attributes['project_id'], $node);
			}
            return stored($node);
        } catch (Exception $exception) {
            return internalError();
        }
    }

    /**
     * 更新节点信息
     *
     * Date: 22/04/2018
     * @author George
     * @param Request $request
     * @param Node $node
     * @return \Illuminate\Http\JsonResponse
     */
    public function update(Request $request, Node $node)
    {
        $attributes = $this->validate($request, [
            'project_id' => 'nullable',
            'circle_id' => 'nullable',
            'name' => 'required',
            'location' => 'required',
            'images' => 'nullable|array',
			'destroy' => 'nullable|array'
        ]);
        $user = Auth::user();

        $images = array_pull($attributes, 'images', []);
        $destroy = array_pull($attributes, 'destroy');

		try {
			$imageRepository = new ImageRepository();
			$imagesPath = $imageRepository->store($images, $user->id, $user->company_id, $user->project_id, $node);
			// 如果需要删除
			if (count($destroy) > 0) {
				foreach ($destroy as $id) {
					/**
					 * @var Image $image
					 */
					$image = Image::findOrFail($id);
					$imageRepository->destroy($image);
				}
			}
			$node->update($attributes);
			$node->images = $imagesPath;
			return updated($node);
		} catch (Exception $exception) {
			return internalError('更新失败');
		}
	}

    /**
     * 删除巡检节点
     *
     * Date: 22/04/2018
     * @author George
     * @param Node $node
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function destroy(Node $node)
    {
        $node->delete();
        return deleted();
    }
}
